package com.lvy.hczn.front.system.service.impl;

import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lvy.hczn.front.common.core.text.Convert;
import com.lvy.hczn.front.common.exception.UtilException;
import com.lvy.hczn.front.system.domain.system.SysArea;
import com.lvy.hczn.front.system.mapper.SysAreaMapper;
import com.lvy.hczn.front.system.service.ISysAreaService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.Wrapper;
import java.util.*;
import java.util.stream.Collectors;

@Service
@Slf4j
public class SysAreaServiceImpl extends ServiceImpl<SysAreaMapper, SysArea> implements ISysAreaService {

    @Autowired
    private SysAreaMapper sysAreaMapper;

    /**
     * 获取分页列表
     *
     * @param sysArea
     * @return
     */
    @Override
    public List<SysArea> getPageList(SysArea sysArea) {
        return sysAreaMapper.getPageList(sysArea);
    }

    @Override
    public SysArea getById(String id) {
        if (StrUtil.isEmpty(id)) {
            throw new UtilException("ID为空");
        }
        return sysAreaMapper.selectById(id);
    }

    @Override
    public int saveData(SysArea sysArea) throws Exception {
        return sysAreaMapper.insert(sysArea);
    }

    @Override
    public void delData(String ids) throws Exception {
        String[] idArray = Convert.toStrArray(ids);
        for (String id : idArray) {
            sysAreaMapper.deleteById(id);
        }
    }

    /**
     * @param id 子节点ID
     * @description: 根据子节点ID查询所有父节点
     * @author yw
     * @date 2021/4/24 16:43
     * @Return
     */
    @Override
    public List<SysArea> getParentNodesByChildNode(String id) throws Exception {
        if (StrUtil.isEmpty(id)) {
            throw new UtilException("参数为空");
        }
        return sysAreaMapper.getParentNodesByChildNode(id);
    }

    /**
     * @param pId
     * @description: 根据父节点查询子节点列表
     * @author yw
     * @date 2021/4/24 17:53
     * @Return
     */
    @Override
    public List<SysArea> getChildNodeByPId(String pId) throws Exception {
        return this.list(Wrappers.<SysArea>lambdaQuery().eq(SysArea::getPid, pId));
    }

    /**
     * @param extName
     * @description: 根据统称查询
     * @author yw
     * @date 2021/4/24 17:53
     * @Return
     */
    @Override
    public List<SysArea> getByExtName(String extName) throws Exception {
        if (StrUtil.isEmpty(extName)) {
            throw new UtilException("参数为空");
        }
        return this.list(Wrappers.<SysArea>lambdaQuery().like(SysArea::getExtName, extName));
    }

    /**
     * 省市区N（deep）级联动数据返回，根据deep字段设置,deep父级信息（包含deep级）
     *
     * @param sysArea
     * @return
     */
    @Override
    public List<SysArea> selectByDeep(SysArea sysArea) {
        int deep = 2;
        if (sysArea.getDeep() == null) {
            sysArea.setDeep(deep);
        }
        List<SysArea> list = super.list(Wrappers.<SysArea>lambdaQuery().le(SysArea::getDeep, sysArea.getDeep()).gt(SysArea::getExtId, 0).orderByAsc(SysArea::getDeep).orderByAsc(SysArea::getExtId));

        //根据deep组装
        LinkedHashMap<Integer, List<SysArea>> mapDeep = list.stream().collect(Collectors.groupingBy(SysArea::getDeep, LinkedHashMap::new, Collectors.toList()));

        //根据pid组装
        LinkedHashMap<String, List<SysArea>> mapPid = list.stream().collect(Collectors.groupingBy(SysArea::getPid, LinkedHashMap::new, Collectors.toList()));

        //子集绑定（deep-1）层父级
        mapDeep.forEach((k, v) -> {
            if (k < deep) {
                v.forEach(item -> {
                    item.setSub(mapPid.get(item.getId()));
                });
            }
        });
        return list;
    }
}
